/**
 * Copyright (c) 2015 www.smarthome.com All rights reserved. <br />
 * 本软件源代码版权归xxxx公司,未经许可不得任意复制与传播.<br />
 *
 */
package com.smarthome.core.user.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.service.ShPropertyCompanyService;
import com.smarthome.base.service.ShPropertyEmployeeService;
import com.smarthome.base.service.ShUserService;
import com.smarthome.core.user.PropertyEmployeeService;
import com.smarthome.domain.bean.ShPropertyCompany;
import com.smarthome.domain.bean.ShPropertyEmployee;
import com.smarthome.domain.bean.ShUser;
import com.smarthome.domain.enums.CredentialType;
import com.smarthome.domain.enums.SexEnum;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;



@Service("propertyEmployeeService")
public class PropertyEmployeeServiceImpl implements PropertyEmployeeService {

    protected static Logger LOGGER;

    @Resource(name="shPropertyEmployeeService")
    private ShPropertyEmployeeService shPropertyEmployeeService;
    
    @Resource(name="shPropertyCompanyService")
    private ShPropertyCompanyService shPropertyCompanyService;

    @Resource(name="shUserService")
    private ShUserService userService;

    public PropertyEmployeeServiceImpl() {
        LOGGER = LoggerFactory.getLogger(this.getClass());
    }

    @Override
    public ShPropertyEmployee queryPropertyEmployeeById(long employeeId) {
        return shPropertyEmployeeService.selectEntryById(employeeId);
    }

	@Override
	public DataGrid datagrid(ShPropertyEmployee employee,PageHelper page) {
		DataGrid grid = shPropertyEmployeeService.dataGrid(employee, page);
		if(null != grid){
			grid.setRows(addParams(grid.getRows()));
		}
		return grid;
	}

	@Override
	public List<ShPropertyEmployee> PropertyEmployeesByPropertyId(
			Long propertyId) {
		
		
		return null;
	}

	@Override
	public int delete(Long id) {
		ShUser user = new ShUser();
		user.setEmployeeId(id);
		List<ShUser> users= userService.selectEntryList(user);
		if(null != users && users.size() > 0) {
			throw new  RuntimeException("已关联用户不能删除");
		}
		
		return shPropertyEmployeeService.deleteByIds(id);
	}

	@Override
	public int add(ShPropertyEmployee employee) {
		
		return shPropertyEmployeeService.insert(employee);
	}

	@Override
	public int eidt(ShPropertyEmployee employee) {
		ShPropertyEmployee queryEmployee  = shPropertyEmployeeService.selectEntryById(employee.getId());
		
		if(null == queryEmployee){
			return 0;
		}
		
		BeanUtils.copyProperties(employee, queryEmployee);
			
		return shPropertyEmployeeService.saveOrUpdate(queryEmployee);
	}

	@Override
	public List<ShPropertyEmployee> addParams(List<ShPropertyEmployee> employees) {
		if(null == employees || employees.size() == 0) return employees;
		
		//获取物业公司id
		Set<Long> companyIds = new HashSet<Long>();
		for(int i = 0 ; i<employees.size();i++){
			ShPropertyEmployee employee = 	employees.get(i);
			//添加性别
			employee.setSexStr(SexEnum.getNameByCode(employee.getSex()));
			//添加卡种
			employee.setCertStr(CredentialType.getNameByCode(employee.getCertType()));
			
			if(null != employees.get(i).getCompanyId()){
				companyIds.add(employees.get(i).getCompanyId());
			}
		}
	
		List<ShPropertyCompany>companys = shPropertyCompanyService.selectEntryListByIds(companyIds.toArray(new Long[]{}));
		
		if(null == companys){
			return employees;
		}
		
		Map<Long,ShPropertyCompany> map = new HashMap<Long,ShPropertyCompany>();
		for(ShPropertyCompany c : companys){
			map.put(c.getId(), c);
		}
		
		//设置
		for(ShPropertyEmployee employee : employees){
			ShPropertyCompany c = map.get(employee.getCompanyId());
			if(null != c){
				employee.setCompany(c);
				employee.setCompanyName(c.getName());
			}
		}
		
		return employees;
	}

	@Override
	public boolean isReapet(ShPropertyEmployee employee) {
		if(null == employee  || StringUtils.isBlank(employee.getName())){
			return false;
		}
		
		
		ShPropertyEmployee qe = new ShPropertyEmployee();
		
		qe.setName(employee.getName());
		qe.setCompanyId(employee.getCompanyId());
		
		List<ShPropertyEmployee> ps = shPropertyEmployeeService.selectEntryList(qe);
		if(null == ps || ps.size() == 0){
			return false;
		}
		
		for(ShPropertyEmployee s : ps ){
			if((s.getName() == null  && employee.getName() == null)||s.getName().equals(employee.getName())  ){
				if(employee.getId() != null && s.getId().equals(employee.getId())) continue;
				return true;
			}
		}
		
		return false;
	}

}
